package com.abigdreamer.swing.app.skin;

import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.app.SwingUtil;
import com.abigdreamer.swing.component.JCButton;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;

public class SkinUnit
  extends JComponent
{
  private static final long serialVersionUID = 4145544490659464008L;
  private static final Color SHADOW_COLOR = new Color(255, 255, 255, 60);
  private static final Color SELECTED_COLOR = new Color(255, 255, 255);
  private SkinMgr skinMgr;
  private Skin skin;
  private JCButton btnAdd;
  private JCButton btnRemove;
  private EmptyComponent ec;
  private boolean first;
  private boolean last;
  private boolean mouseIn;
  
  public SkinUnit(SkinMgr skinMgr)
  {
    this.skinMgr = skinMgr;
    this.btnAdd = new JCButton();
    this.btnRemove = new JCButton();
    this.ec = new EmptyComponent();
    ActionListener action = new ActionHandle();
    MouseListener mouseHandle = new MouseHandle();
    init();
    addMouseListener(mouseHandle);
    this.btnAdd.addActionListener(action);
    this.btnAdd.addMouseListener(mouseHandle);
    this.btnRemove.addActionListener(action);
    this.btnRemove.addMouseListener(mouseHandle);
  }
  
  private void init()
  {
    setLayout(new BorderLayout());
    this.ec.setLayout(new BorderLayout());
    this.ec.setPreferredSize(new Dimension(-1, 13));
    this.ec.setVisible(false);
    this.btnAdd.setToolTipText("请点击选择一张图片，生成属于您自己风格的皮肤。");
    
    this.btnAdd.setFocusable(false);
    this.btnAdd.setImage(SwingUtil.getImage("skin_add_normal.png", true));
    this.btnAdd.setRolloverImage(SwingUtil.getImage("skin_add_rollover.png", true));
    this.btnAdd.setPressedImage(this.btnAdd.getRolloverImage());
    this.btnAdd.setVisible(false);
    this.btnRemove.setToolTipText("删除");
    this.btnRemove.setPreferredSize(new Dimension(13, -1));
    this.btnRemove.setRolloverEnabled(false);
    this.btnRemove.setFocusable(false);
    this.btnRemove.setImage(SwingUtil.getImage("skin_delete.png", true));
    this.btnRemove.setPressedImage(null);
    this.ec.add(this.btnRemove, "East");
    add(this.btnAdd, "Center");
    add(this.ec, "North");
  }
  
  protected void paintComponent(Graphics g)
  {
    if (this.skin != null)
    {
      int width = getWidth();
      int height = getHeight();
      Image image = this.skin.getPreviewImage();
      g.drawImage(image, 0, 0, width, height, this);
      image = null;
      if (this.mouseIn)
      {
        g.setColor(SHADOW_COLOR);
        g.fillRect(0, 0, width, height);
      }
      if (this.skin.isSelected())
      {
        g.setColor(SELECTED_COLOR);
        g.drawRect(0, 0, width - 1, height - 1);
      }
    }
  }
  
  public Skin getSkin()
  {
    return this.skin;
  }
  
  public void setSkin(Skin skin)
  {
    this.skin = skin;
    setToolTipText(skin != null ? skin.getName() : null);
    this.btnAdd.setVisible((this.last) && (skin == null));
    repaint();
    if (skin != null)
    {
      skin.setUnit(this);
      setFirst(skin.isDefault());
    }
  }
  
  public boolean isFirst()
  {
    return this.first;
  }
  
  public void setFirst(boolean first)
  {
    this.first = first;
  }
  
  public boolean isLast()
  {
    return this.last;
  }
  
  public void setLast(boolean last)
  {
    this.last = last;
    this.btnAdd.setVisible((last) && (this.skin == null));
  }
  
  private class ActionHandle
    implements ActionListener
  {
    private ActionHandle() {}
    
    public void actionPerformed(ActionEvent e)
    {
      Object source = e.getSource();
      if (source == SkinUnit.this.btnAdd) {
        SwingUtil.switchToSystemLAFTemp(new AbstractAction()
        {
          private static final long serialVersionUID = 5648508246819086152L;
          
          public void actionPerformed(ActionEvent e)
          {
            SkinUnit.this.skinMgr.addSkin();
          }
        });
      } else if (source == SkinUnit.this.btnRemove) {
        SkinUnit.this.skinMgr.removeSkin(SkinUnit.this.skin, true);
      }
    }
  }
  
  private class MouseHandle
    extends MouseAdapter
  {
    private MouseHandle() {}
    
    public void mouseEntered(MouseEvent e)
    {
      Object source = e.getSource();
      SkinUnit.this.mouseIn = true;
      SkinUnit.this.ec.setVisible((SkinUnit.this.skin != null) && (!SkinUnit.this.first));
      if (SkinUnit.this.skin != null) {
        SkinUnit.this.repaint();
      }
      if ((source == SkinUnit.this) || (source == SkinUnit.this.btnAdd)) {
        SkinManager.changeSkin(SkinUnit.this.skin == null ? SkinManager.getCurrentSkin() : SkinUnit.this.skin, false);
      }
    }
    
    public void mouseExited(MouseEvent e)
    {
      Object source = e.getSource();
      SkinUnit.this.mouseIn = false;
      SkinUnit.this.ec.setVisible(false);
      if (SkinUnit.this.skin != null)
      {
        SkinUnit.this.repaint();
        if (((source == SkinUnit.this) || (source == SkinUnit.this.btnRemove)) && (SkinUnit.this.skinMgr.needChange(e.getPoint(), (Container)source))) {
          SkinManager.changeSkin(SkinManager.getCurrentSkin(), false);
        }
      }
    }
    
    public void mousePressed(MouseEvent e)
    {
      if ((SkinUnit.this.skin != null) && (e.getSource() == SkinUnit.this) && (SwingUtilities.isLeftMouseButton(e))) {
        SkinManager.changeSkin(SkinUnit.this.skin, true);
      }
    }
  }
}
